/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.ecommerce.factory;

import br.com.ecommerce.exceptions.DatabaseException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author Afonso
 */
public class DatabaseFactory {

    private static Connection con = null;

    private static Properties loadPropertiesFile() throws DatabaseException {
        Properties properties = new Properties();
        try {
            //Setamos o arquivo que será lido
            properties.load(ClassLoader.getSystemResourceAsStream("JDBC.properties"));
            return properties;
            
           } catch (IOException e) {
                throw new DatabaseException(e.getMessage(),
                        e.getCause());
        }
    
    }


    public static Connection getConnection() throws DatabaseException {
//        if (con == null) {
            try {
               Properties props = loadPropertiesFile();

               OracleDataSource ds = new OracleDataSource();
               ds.setURL(props.getProperty("jdbc.url"));
               con = ds.getConnection(props.getProperty("jdbc.user"),
                                       props.getProperty("jdbc.password"));

            } catch (SQLException ex) {
              throw new DatabaseException("Mensagem:" + ex.getMessage(), ex.getCause());

            }
//        }
        return con;
        
    }

}
